﻿using HomagChina.CCT.DataTypeModel;
using HomagGroup.Base;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using System.Windows.Shapes;
using static HomagChina.CCT.ProcessResourceInternal;

namespace HomagChina.CCT.ViewModels
{
    public class HomeViewModel : NotifyPropertyChangedBase
    {
        private Canvas _DeviceCanvas = new Canvas();

        public Canvas DeviceCanvas
        {
            get => _DeviceCanvas;
            set { _DeviceCanvas = value; OnPropertyChanged(); }
        }

        private string _ProjectNmae = "";

        public string ProjectNmae
        {
            get => _ProjectNmae;
            set
            {
                _ProjectNmae = value; OnPropertyChanged();
            }
        }


        public DropDownList dropDownList { get; set; } = new DropDownList();
        private List<MachineInfoDataModel> machineInfoList = new List<MachineInfoDataModel>();

        public List<MachineInfoDataModel> MachineInfoList
        {
            get => machineInfoList;

            set
            {
                machineInfoList = value;
                OnPropertyChanged();
            }
        }

        public HomeViewModel()
        {
            dropDownList.ItemsSource = new List<String>() { { "Auto" }, { "Project" } };
        }

        public void HandleLoadProject()
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "CSV Files|*.csv",
                Title = "打开项目文件"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                if (MachineInfoList.Count != 0) { MachineInfoList.Clear(); }
                string templatePath = openFileDialog.FileName;
                using (StreamReader reader = new StreamReader(templatePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] array = line.Split(',');
                        if (array[0] == "REFERENCE") { continue; }
                        MachineInfoDataModel machineInfoDataModel = new MachineInfoDataModel();

                        machineInfoDataModel.Reference = array[0];
                        machineInfoDataModel.DeviceType = array[1];
                        machineInfoDataModel.Gap = array[2].ToDouble();
                        machineInfoDataModel.Belt = array[3].ToDouble();
                        machineInfoDataModel.Width = array[4].ToDouble();
                        machineInfoDataModel.Height = array[5].ToDouble();
                        machineInfoDataModel.Length = array[6].ToDouble();
                        machineInfoDataModel.RollerQuantity = array[7].ToInt32();
                        machineInfoDataModel.InfeedSensor = array[8].ToBoolean();
                        machineInfoDataModel.CreepSensor = array[9].ToBoolean();
                        machineInfoDataModel.StopSensor = array[10].ToBoolean();
                        machineInfoDataModel.LSideObstruction = array[11].ToBoolean();
                        machineInfoDataModel.SSideObstruction = array[12].ToBoolean();
                        machineInfoDataModel.MachineHeight = array[13].ToDouble();
                        machineInfoDataModel.MachineWidth = array[14].ToDouble();
                        machineInfoDataModel.MachineCode = array[15].ToString();
                        machineInfoDataModel.Xposition = array[16].ToDouble();
                        machineInfoDataModel.Yposition = array[17].ToDouble();
                        machineInfoDataModel.MachineImageOpactity = array[18].ToDouble();
                        machineInfoDataModel.MachineImagePath = array[19].ToString();
                        ProjectNmae = array[20];

                        MachineInfoList.Add(machineInfoDataModel);
                    }
                }
            }
            foreach (var item in MachineInfoList)
            {
                Rectangle ImageRectangle = new Rectangle
                {
                    Uid = item.Reference,
                    Width = item.MachineWidth,
                    Height = item.MachineHeight,
                    Fill = BitMapConvertsImageBrush(item.MachineImagePath),
                    Opacity = item.MachineImageOpactity,
                    Focusable = true
                };
                Canvas.SetLeft(ImageRectangle, item.Xposition);
                Canvas.SetTop(ImageRectangle, item.Yposition);
                DeviceCanvas.Children.Add(ImageRectangle);
                DeviceCanvas.InvalidateVisual();
            }
        }
    }
}